package com.hisan.freeride.common.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hisan.freeride.R;

import java.util.Date;

public class LoadingLayout extends FrameLayout {
	static final int DEFAULT_ROTATION_ANIMATION_DURATION = 150;
	private final ImageView headerImage;
	private final ProgressBar headerProgress;
	private final TextView headerText;
	private String pullLabel;
	private String refreshingLabel;
	private String releaseLabel;
	private Date data;
	private final Animation rotateAnimation, resetRotateAnimation;

	public LoadingLayout(Context context, final int mode, String releaseLabel, String pullLabel, String refreshingLabel, Date d) {
		super(context);
		ViewGroup header = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header, this);
		headerText = (TextView) header.findViewById(R.id.pull_to_refresh_text);
		headerImage = (ImageView) header.findViewById(R.id.pull_to_refresh_image);
		headerProgress = (ProgressBar) header.findViewById(R.id.pull_to_refresh_progress);
//		dateText= (TextView) header.findViewById(R.id.pull_to_refresh_updated_at);
		final Interpolator interpolator = new LinearInterpolator();
		rotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
		        0.5f);
		rotateAnimation.setInterpolator(interpolator);
		rotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
		rotateAnimation.setFillAfter(true);

		resetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
		        Animation.RELATIVE_TO_SELF, 0.5f);
		resetRotateAnimation.setInterpolator(interpolator);
		resetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
		resetRotateAnimation.setFillAfter(true);

		this.releaseLabel = releaseLabel;
		this.pullLabel = pullLabel;
		this.refreshingLabel = refreshingLabel;
		this.data=d;
		switch (mode) {
			case PullToRefreshView.MODE_PULL_UP_TO_REFRESH:
				headerImage.setImageResource(R.mipmap.refresh_arrow_down);
				break;
			case PullToRefreshView.MODE_PULL_DOWN_TO_REFRESH:
			default:
				headerImage.setImageResource(R.mipmap.refresh_arrow_up);
				break;
		}
	}
	public void reset() {
		headerText.setText(pullLabel);
		headerImage.setVisibility(View.VISIBLE);
		headerProgress.setVisibility(View.GONE);

//		setLastUpdated("最近更新:" + data.toLocaleString());
	}

	public void releaseToRefresh() {
		headerText.setText(releaseLabel);
		headerImage.clearAnimation();
		headerImage.startAnimation(rotateAnimation);
	}
//
//	public void setPullLabel(String pullLabel) {
//		this.pullLabel = pullLabel;
//	}

	public void refreshing() {
		headerText.setText(refreshingLabel);
		headerImage.clearAnimation();
		headerImage.setVisibility(View.INVISIBLE);
		headerProgress.setVisibility(View.VISIBLE);
	}
//
//	public void setRefreshingLabel(String refreshingLabel) {
//		this.refreshingLabel = refreshingLabel;
//	}
//
//	public void setReleaseLabel(String releaseLabel) {
//		this.releaseLabel = releaseLabel;
//	}

	public void pullToRefresh() {
		headerText.setText(pullLabel);
		headerImage.clearAnimation();
		headerImage.startAnimation(resetRotateAnimation);
	}

	public void setTextColor(int color) {
		headerText.setTextColor(color);
	}
//	public void setLastUpdated(CharSequence lastUpdated) {
////		if (lastUpdated != null) {
////			dateText.setVisibility(View.VISIBLE);
////			dateText.setText(lastUpdated);
////		} else {
////			dateText.setVisibility(View.GONE);
////		}
//	}

}
